/***
 * **********************************************************
 * @author:liangjilong
 * @version:1.0
 * @fileoverview:邮件管理
 * @Email:liangjilong51job@qq.com
 * **********************************************************
 * flag 1详情 2修改 3删除
 */
$(document).ready(function(){
	 
     /*******************************Grid****End**************************************/
     InitiDataSource();
   
    /**================================查询=Begin===============================*/
    $("#btnSearch").click(function(){
    	btnSearch();
    });
	 
	/****
	 * ***************************************************修改按钮*******************************************
	 */
   $("#btnModify").click(function(){
	    /*********************提示信息*Begin**************************/
		 var gridView = $("#gridView").data("kendoGrid");
	     if(gridView.select().length==0||gridView.select().length>1){
	   		Morinda.alert("请选择一条记录");
	        return;
	     }
	     var selectedItem = gridView.dataItem(gridView.select());
	     var Id=$.trim(selectedItem.Id);
	     $("#has_EmailId").val(Id);
	     showDetail(Id,"修改",2);
	     $("#submit_save_Id").hide();
   });
   	
	/***
	 * 确定修改
	 */
   $("#submit_OK_Id").click(function(){
   		 var hasEmailId=$.trim($("#has_EmailId").val());
   		 var email=$.trim($("#update_Email").val());
	   	 $.ajax({
			type: "POST", 
			url: _basePath+"updateEmailById.do", 
	   		dataType: "json", 
	   		data:{emailId:hasEmailId,email:email},
	   		success: function(responseText){
	   			 var flag=responseText.flag;
	   			 if(flag==true){
			         closeReturnMessageWindow("detailWindow","call_msg_content_Id","修改成功","call_msg_WindId","消息","btnSearch");
	   			 }else{
	   			 	 closeReturnMessageWindow("detailWindow","call_msg_content_Id","修改失败","call_msg_WindId","消息","btnSearch");
	   			 }
	   		}
   		});
   });
   
    /**
     *******************************************************************************************************
     * 添加
     */
    $("#btnInsert").click(function(){
    	 var email=$.trim($("#update_Email").val());
    	 if(email!=null||email==""){
    	 	$("#update_Email").val("");
    	 }
    	 showDialog('detailWindow', 320, 150, '添加');
    	 $("#submit_OK_Id").hide();
    	 $("#submit_save_Id").show();
    });
    
    $("#submit_save_Id").click(function(){
    	
   		 var email=$.trim($("#update_Email").val());
	   	 $.ajax({
			type: "POST", 
			url: _basePath+"insertEmail.do", 
	   		dataType: "json", 
	   		data:{email:email},
	   		success: function(responseText){
	   			 var flag=responseText.flag;
	   			 var status=responseText.status;
	   			 if(flag==false&&status==false){
	   			 	closeReturnMessageWindow("detailWindow","call_msg_content_Id","Email已经存在!","call_msg_WindId","消息","btnSearch");
	   			 }else if(flag==true&&status==true){
	   			 	closeReturnMessageWindow("detailWindow","call_msg_content_Id","添加成功!","call_msg_WindId","消息","btnSearch");
	   			 }else{
	   			 	closeReturnMessageWindow("detailWindow","call_msg_content_Id","添加失败","call_msg_WindId","消息","btnSearch");
	   			 }
	   		}
   		});
    });
    
    
    
     /**
     *******************************************************************************************************
     * 删除
     */
    $("#btnDelete").click(function(){
    	 var gridView = $("#gridView").data("kendoGrid");
	     if(gridView.select().length==0||gridView.select().length>1){
	   		Morinda.alert("请选择一条记录");
	        return;
	     }
	     var selectedItem = gridView.dataItem(gridView.select());
	     var Id=$.trim(selectedItem.Id);
	     deleteEmail(Id)
	     
    });
    
   
});
  
  
 /**************************************************************function*******************************************/  

function deleteEmail(id){
	 $.ajax({
		type: "POST", 
		url: _basePath+"deleteEmailById.do", 
   		dataType: "json", 
   		data:{id:id},
   		success: function(responseText){
   			 
   			 if(responseText.flag==true){
   			 		closeReturnMessageWindow("detailWindow","call_msg_content_Id","成功删除","call_msg_WindId","消息","btnSearch");
   			 		$("#call_msg_btn_Id").click(function(){
   			 			document.location.reload();//重新刷新页面
   			 		})
   			 } else{
   			 		closeReturnMessageWindow("detailWindow","call_msg_content_Id","删除失败","call_msg_WindId","消息","btnSearch");
   			 }
   			
   		}
   	});
	
}

/****
 * 显示详情
 */
function showDetail(Id,titile,flag){
	 $.ajax({
		type: "POST", 
		url: _basePath+"getEmailById.do", 
   		dataType: "json", 
   		data:{emailId:Id},
   		success: function(responseText){
   			 var email=$.trim(responseText.Email);
   			 $("#update_Email").val(email);
   			 showDialog("detailWindow","320","120",titile);// showDialog(displayDiv, w, h, title)
   			
   			 if(flag=="1"){
   			 	 $('#submit_OK_Id').hide();
   			 	 $("#submit_save_Id").hide();
   			 	 $('#update_Email').attr("readonly","readonly");
   			 	 $("#update_Email").attr("disabled",true);   
   			 }else if(flag=="2"){
   			 	 $('#update_Email').removeAttr("readonly");//移除
   			 	 $('#update_Email').removeAttr("disabled");
   			 	 $('#submit_OK_Id').show();
   			 	 $("#submit_save_Id").hide();
   			 }
   			
   		}
   	});
};

/******************************初始化控件*Begin******************************/
function InitiDataSource(){
	/******************************初始化Grid控件*******************************/
     var dataSource = new kendo.data.DataSource({
	  transport: {					
	    read: {
	    	  type: "POST", 
     	      dataType: "jsonp",//url?callback
     	      url: _basePath+"getEmailLists.do",
     	      data:{}
	    }
	  },
	  schema: {
		   data: "data",
		   total:"total",
		   parse:function(response){
			  for(var i=0;i<response.data.length;i++){
				  response.data[i]["_rowNumber"]=(i+1);							   
			  }
			  return response;
		   }
	  }, 
	  parameterMap : function(options, operation) {
        if (operation == "read") {
          var parameter = {
            page : options.page, //当前页
            pageSize : options.pageSize//每页显示个数
          };
          return kendo.stringify(parameter);
        }
      },
	  pageSize: 15,
      serverPaging: true,
      serverFiltering: true,
      serverSorting: true       
	});
	  
	  /*******************************Grid****Begin**************************************/
	  $("#gridView").kendoGrid({
         dataSource:dataSource,
         height: 600,       
         sortable: true,
         pageable:{
			 refresh:true,
			 pageSizes:true,
			 buttonCont:5
		 },   
         resizable: true,
         selectable: "multiple, row",
         columns:[			
			{width: 50,title:  "NO.",field:"_rowNumber"},
			{title: "邮件", field : "Email", width : 180,template: function(record){
				var msg="<a style='cursor:pointer;color: blue;'";
					msg+="onclick='javascript:showDetail(\"" + record.Id + "\",\"详情\",\"1\");'>"
					msg+=record.Email+"</a>";
				return msg;
			}},	
			{title: "创建时间", field : "createTime", width : 180},
			{title: "修改时间", field : "modifyTime", width : 180}
			/*{command:[{
					   name: "删除",
					   width:50,
					   click:function(e){
					   		// get the current table row (tr)获取当前表格的行
					   	    var tr = $(e.target).closest("tr"); 
					   	    var data = this.dataItem(tr);
					   	    deleteEmail(data.Id);
					   }
				}]
			}*/
        ]
     }); 
}
/******************************初始化控件*End******************************/

/**================================查询=Begin===============================*/
function btnSearch(){
	 var conditions=[];
	 var Email=$.trim($("#Email").val());
	 if(Email!="")
	 {
	 	 var item={
			 columnName:"Email",
			 dataType:SQLConditionParams.DATA_TYPE_STRING, //指定数据类型为字符串
			 value:Email,
			 queryType:SQLConditionParams.QUERY_TYPE_VAGUE //指定查询类型为模糊查询
	 	};
	    conditions.push(item);
	 }			            	
	 var createTime=$.trim($("#createTime").val());
	
	 if(createTime!="")
	 {
	 	 var item={
			 columnName:"createTime",
			 dataType:SQLConditionParams.DATA_TYPE_STRING, //指定数据类型为字符串
			 value:createTime,
			 queryType:SQLConditionParams.QUERY_TYPE_VAGUE //指定查询类型为模糊查询
	 	};
	    conditions.push(item);
	 }	            	
     //将查询条件设置到数据源,stringify是json2.js里面的方法	
	 $("#gridView").data("kendoGrid").dataSource.options.transport.read.data.conditions=JSON.stringify(conditions);	
     //数据源将向服务器发布请求
	 $("#gridView").data("kendoGrid").dataSource.read();		
     //刷新表格
	 $("#gridView").data("kendoGrid").refresh();
}
/**================================查询=End===============================*/
 